package com.comp490.sprite;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;

import com.comp490.bullet.Bullet;
import com.comp490.bullet.RoundBullet;
import com.comp490.pattern.CircularPattern;
import com.comp490.pattern.ConePattern;
import com.comp490.shooter.GameView;

public class LargeEnemySprite extends EnemySprite
{
    private int bitmapHeight;
    private int bitmapWidth;
    private float rotationSpeed = 0.5f;
    private boolean moveLeft = true;
    
    public LargeEnemySprite(GameView gameView, int enemyColor, int path, float originalX, float originalY)
    {
    	super(gameView, 150, 10000);

        Bitmap enemyShip = BitmapResource.LARGE_ENEMY_SHIP;

        bitmapHeight = enemyShip.getHeight();
        bitmapWidth = (int) ((enemyShip.getWidth() / 7.0) + 0.5);
        setBitmap(enemyShip, bitmapWidth, bitmapHeight);

        spriteTop = 0;//enemyColor * bitmapHeight;

        collisionRadius = bitmapHeight < bitmapWidth ? bitmapHeight / 3
                : bitmapWidth / 3;

        src = new Rect(spriteLeft, spriteTop, spriteLeft
                + bitmapWidth, spriteTop + bitmapHeight);
        
        dst = new Rect((int) x, (int) y, (int) x + bitmapWidth,
                (int) y + bitmapHeight);
        this.path = path;
        firePoint = 4;
        buildBulletPatterns();
    }

	@Override
	public void updatePosition()
	{
		if(path == MOVE_STRAIGHT && moveLeft)
		{
			moveLine(2, 180);
			if(x  == (gameView.getWidth() - bitmapWidth)/2)
			{
				moveLeft = false;
				reset();
			}
			
		}
		else if(path == MOVE_STRAIGHT && !moveLeft)
		{
			moveLine(2, 360);
			if(x > gameView.getWidth() - bitmapWidth)
			{
				moveLeft = true;
				reset();
			}
			
		}
			
		if(!boundsCheck(gameView))
		{
			active = false;
		}
		checkFirePos();		
	}

	@Override
	public void draw(Canvas canvas)
	{
		if(!explode)
        {
			if(!active)
				return;
			
			src.set(spriteLeft, spriteTop, spriteLeft
                    + bitmapWidth, spriteTop + bitmapHeight);
            dst.set((int) x, (int) y, (int) x + bitmapWidth,
                    (int) y + bitmapHeight);

            canvas.save();
            canvas.rotate(rotationSpeed++, x + bitmapWidth / 2, y
                    + bitmapHeight / 2);
            canvas.drawBitmap(bitmap, src, dst, shipIsHit ? BitmapResource.HIT_INDICATOR : null);
            canvas.restore();

            if(shipIsHit)
            	shipIsHit = false;
            if(fCount++ % 6 == 0)
            {
                if(spriteLeft + bitmapWidth >= bmpWidth)
                    spriteLeft -= 3 * bitmapWidth;
                else
                    spriteLeft += bitmapWidth;
            }
        }
        else
        {
            explode = mExplosionSprite.draw(canvas, x, y, ExplosionSprite.EXPLOSION_SEVEN);
        }
		
	}

	@Override
	public void reset()
	{
        buildBulletPatterns();
        firePoint = 4;	
	}

	public void fireArtillery()
	{
		for(int i = 0; i < 2; i++)
		{
			bulletPatterns.get(0).setCoordinates(getCenterBitmapX()-20,
					getCenterBitmapY());
			gameView.getEnemy_bullet_patterns().add(
					bulletPatterns.get(0));
			bulletPatterns.remove(0);
		}

		firePoint--;
	}
	@Override
	protected void checkFirePos()
	{
		switch (firePoint)
		{
			case 4:
				if (x < screenWidth * 7 / 8 && moveLeft)
				{
					fireArtillery();
				}
				else if (x < screenWidth * 4 / 8 && !moveLeft)
				{
					firePoint--;
				}
				break;
				
			case 3:
				if (x < screenWidth * 6 / 8 && moveLeft)
				{
					fireArtillery();
				}
				else if (x < screenWidth * 5 / 8 && !moveLeft)
				{
					fireArtillery();
				}
				break;
			case 2:
				if (x < screenWidth * 5 / 8 && moveLeft)
				{
					fireArtillery();
				}
				else if (x < screenWidth * 6 / 8 && !moveLeft)
				{
					fireArtillery();
				}
				break;
			case 1:
				if (x < screenWidth * 4 / 8 && moveLeft)
				{
					fireArtillery();
				}
				else if (x < screenWidth * 7 / 8 && !moveLeft)
				{
					fireArtillery();
				}
				break;
			default:
					break;
		}		
	}

	@Override
	protected void buildBulletPatterns()
	{
		for (int i = 0; i < 4; i++)
		{
			List<Bullet> bullets = new ArrayList<Bullet>();
			for (int j = 0; j < 15; j++)
			{
				Bullet b = new RoundBullet(0, 0, 1, BitmapResource.RED_PAINT,
						6, false);
				b.setActive(true);
				bullets.add(b);
			}

			CircularPattern circ = new CircularPattern(2, bullets, gameView, true);
			bulletPatterns.add(circ);
					
			bullets = new ArrayList<Bullet>();
			for (int j = 0; j < 10; j++)
			{
				Bullet b = new RoundBullet(0, 0, 1, BitmapResource.RED_PAINT,
						6, false);
				b.setActive(true);
				bullets.add(b);
			}
			
			ConePattern cone = new ConePattern(4, bullets, gameView, true, 60, 180);
			bulletPatterns.add(cone);
		}
	}

	@Override
	public float getCenterBitmapX()
	{
		return centerBitmapX + x;
	}

	@Override
	public float getCenterBitmapY()
	{
		return centerBitmapY + y;
	}
}
